package com.tocici.service.impl;

import com.tocici.domain.Music;
import com.tocici.domain.bo.MusicBo;
import com.tocici.domain.vo.ArtistStatVo;
import com.tocici.domain.vo.MusicVo;
import com.tocici.mapper.MusicMapper;
import com.tocici.mapper.UserBehaviorMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.tocici.domain.bo.ArtistBo;
import com.tocici.domain.vo.ArtistVo;
import com.tocici.domain.Artist;
import com.tocici.mapper.ArtistMapper;
import com.tocici.service.IArtistService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 歌手Service业务层处理
 *
 * @author zeratul
 * @date 2025-03-02
 */
@RequiredArgsConstructor
@Service
public class ArtistServiceImpl implements IArtistService {

    private final ArtistMapper baseMapper;

    private final MusicMapper musicMapper;

    private final UserBehaviorMapper userBehaviorMapper;

    /**
     * 查询歌手
     *
     * @param artistId 主键
     * @return 歌手
     */
    @Override
    public ArtistVo queryById(Long artistId){
        return baseMapper.selectVoById(artistId);
    }

    /**
     * 分页查询歌手列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 歌手分页列表
     */
    @Override
    public TableDataInfo<ArtistVo> queryPageList(ArtistBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Artist> lqw = buildQueryWrapper(bo);
        Page<ArtistVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的歌手列表
     *
     * @param bo 查询条件
     * @return 歌手列表
     */
    @Override
    public List<ArtistVo> queryList(ArtistBo bo) {
        LambdaQueryWrapper<Artist> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Artist> buildQueryWrapper(ArtistBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Artist> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Artist::getArtistId);
        lqw.like(StringUtils.isNotBlank(bo.getArtistName()), Artist::getArtistName, bo.getArtistName());
        lqw.eq(StringUtils.isNotBlank(bo.getArtistDescription()), Artist::getArtistDescription, bo.getArtistDescription());
        lqw.eq(bo.getAvatarUrl() != null, Artist::getAvatarUrl, bo.getAvatarUrl());
        lqw.eq(bo.getIsDeleted() != null, Artist::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增歌手
     *
     * @param bo 歌手
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ArtistBo bo) {
        Artist add = MapstructUtils.convert(bo, Artist.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setArtistId(add.getArtistId());
        }
        return flag;
    }

    /**
     * 修改歌手
     *
     * @param bo 歌手
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ArtistBo bo) {
        Artist update = MapstructUtils.convert(bo, Artist.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Artist entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除歌手信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<ArtistVo> search(String artistName) {
        ArtistBo artistBo = new ArtistBo();
        artistBo.setArtistName(artistName);
        List<ArtistVo> artistVos = queryList(artistBo);
        return artistVos;
    }

    @Override
    public List<ArtistStatVo> getHotArtist() {
        ArtistBo artistBo = new ArtistBo();
        List<ArtistVo> artistVos = queryList(artistBo);
        List<ArtistStatVo> artistStatVos = new ArrayList<>();
        artistVos.forEach(vo -> {
            ArtistStatVo artistStatVo = new ArtistStatVo();
            String artistName = vo.getArtistName();
            LambdaQueryWrapper<Music> musicLambdaQueryWrapper = new LambdaQueryWrapper<>();
            musicLambdaQueryWrapper.eq(Music::getArtistId,vo.getArtistId());
            List<MusicVo> musicVos = musicMapper.selectVoList(musicLambdaQueryWrapper);
            List<Long> list = musicVos.stream().map(music -> music.getMusicId()).toList();
            artistStatVo.setName(artistName);
            artistStatVo.setLikeCount(getLikeCount(list));
            artistStatVo.setPlayCount(getPlayCount(vo.getArtistId()));
            artistStatVos.add(artistStatVo);
        });
        return artistStatVos;
    }


    private Integer getLikeCount(List<Long> musicIds){
        return userBehaviorMapper.getLikeCount(musicIds);
    }


    private Integer getPlayCount(Long artistId){
        LambdaQueryWrapper<Music> lqw = new LambdaQueryWrapper<>();
        MusicBo musicBo = new MusicBo();
        musicBo.setArtistId(artistId);
        lqw.eq(Music::getArtistId,artistId);
        List<MusicVo> musicVos = musicMapper.selectVoList(lqw);
        return musicVos.stream().map(MusicVo::getPlayCount).reduce(0, Integer::sum);
    }
}
